<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/1/17
 * Time: 14:49
 */

namespace app\admin\controller;

use app\BaseController;
use Exception;

/**
 *
 */
class BaseAdminController extends BaseController
{
    protected string $validateClass        = '';//验证类
    protected string $searchFields         = '';//查询时需要检索的字段
    protected array  $checkUniqueField     = [];//修改时校验唯一字段
    protected string $fieldSearch          = '';//查询字段约束
    protected string $searchAppendField    = '';//查询时追加的字段
    protected int    $isWithoutGlobalScope = 1;//是否开启全局 0 关闭,1 开启
    protected string $withLink             = "";//关联表
    protected string $searchHiddenField    = "";//查询时需要隐藏的字段
    protected int    $isEach               = 0;//查询时是否组装数据 0 否,1 是
    protected string $orderSort            = "id desc";//排序
    protected string $detailWithLink       = "";//明细需要关联的表

    protected string $store_id = "";//店铺id
    protected mixed  $userInfo;//用户信息
    protected int    $user_id  = 0;//用户id

    public function initialize()
    {
        $this->userInfo = request()->userInfo;
        $this->user_id  = $this->userInfo->id;
    }

    /**
     * 验证字段唯一
     * @param $model
     * @param $fields
     * @param $param
     * @param int $id
     * @return void
     * @throws Exception
     */
    public function uniqueField($model, $fields, $param, int $id = 0): void
    {
        foreach ($fields as $field) {
            if (isset($param[$field])) {
                if (!$this->isWithoutGlobalScope) $model = $model->withoutGlobalScope();
                if ($id) $model = $model->whereNotIn('id', $id);
                if ($model->where($field, $param[$field])->find()) exception($field . '已存在');
            }
        }
    }
}